package com.zsc.manage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zsc.common.annotation.Anonymous;
import com.zsc.common.annotation.Log;
import com.zsc.manage.commom.constant.JwtClaimsConstant;
import com.zsc.manage.commom.properties.JwtProperties;
import com.zsc.manage.commom.result.Result;
import com.zsc.manage.domain.dto.UserLoginDTO;
import com.zsc.manage.domain.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zsc.common.core.controller.BaseController;
import com.zsc.common.core.domain.AjaxResult;
import com.zsc.common.enums.BusinessType;
import com.zsc.manage.domain.entity.User;
import com.zsc.manage.service.IUserService;
import com.zsc.common.utils.poi.ExcelUtil;
import com.zsc.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * @date 2024-12-24
 */
@RestController
@RequestMapping("/manage/user")
@Slf4j
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /*
     *微信登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("微信登录,{}", userLoginDTO.getCode());
        User user = userService.wxLogin(userLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = com.zsc.manage.commom.utils.JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        System.out.println("JWT: " + token);
        System.out.println("Secret Key: " + jwtProperties.getUserSecretKey());
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        log.info("微信登录成功,{}", userLoginVO);
        return Result.success(userLoginVO);
    }

    // 用户信息更新接口示例
    @PutMapping("/{id}")
    public AjaxResult updateUser(@PathVariable Long id, @RequestBody User user) {
        // 实现更新逻辑
        user.setId(id);
        userService.updateUser(user);
        return AjaxResult.success();
    }

    /**
     * 查询用户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('manage:user:list')")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('manage:user:export')")
    @Anonymous
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('manage:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户信息
     */
//    @PreAuthorize("@ss.hasPermi('manage:user:add')")
    @Anonymous
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户信息
     */
//    @PreAuthorize("@ss.hasPermi('manage:user:edit')")
    @Anonymous
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户信息
     */
//    @PreAuthorize("@ss.hasPermi('manage:user:remove')")
    @Anonymous
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
